﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSL.ST
{
    internal class CallFunctionNode : TreeNode
    {
        public CallFunctionNode(TreeNode Parent)
            : base(Parent, TreeNodeType.CallFunction)
        {

        }

        public string FunctionName
        {
            get;
            set;
        }

        public override TreeNode CloneNode()
        {
            CallFunctionNode returnNode = new CallFunctionNode(null);

            returnNode.FunctionName = this.FunctionName;

            return returnNode;
        }

        public override dynamic Execute(PSLEngine Engine)
        {
            List<object> paramList = new List<object>();

            // Get all parameter
            foreach (ST.ParameterNode param in this.FindChildrenOfType<ST.ParameterNode>())
            {
                if (true)//!parameter.ContainsKey(param.ParameterName))
                {
                    //parameter.Add(param.ParameterName, GetParameterValue(param));
                    paramList.Add(param.Execute(Engine));
                }
                else
                {
                    throw new Exception("The parameter " + param.ParameterName + " already used (Function: " + FunctionName + ").");
                }
            }

            // Call function and return the value
            return Engine.Runtime.FunctionStack.Call(FunctionName, paramList.ToArray());
        }
    }
}
